package com.community.zhihuishequ.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.community.zhihuishequ.R;

import io.reactivex.annotations.Nullable;

/**
 * Created by Administrator on 2018/4/26.
 */

public class GamePad extends View {

    /**
     * 控件宽度
     */
    private int width = 320;
    /**
     * 控件高度
     */
    private int height = 320;
    private Paint paint;
    /**
     * 大圆在x轴的坐标
     */
    private float bigCircleX = 160;
    /**
     * 大圆在y轴的坐标
     */
    private float bigCircleY = 160;
    /**
     * 小圆在x轴的坐标
     */
    private float smallCircleX = 160;
    /**
     * 小圆在y轴的坐标
     */
    private float smallCircleY = 160;
    /**
     * 大圆半径
     */
    private float bigCircleR = 120;
    /**
     * 小圆的半径
     */
    private float smallCircleR = 40;
    /**
     * 移动方向监听器
     */
    private OnDirectionListener onDirectionListener;

    public void setOnDirectionListener(OnDirectionListener onDirectionListener) {
        this.onDirectionListener = onDirectionListener;
    }

    public GamePad(Context context) {
        this(context, null);
    }

    public GamePad(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GamePad(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint();
        paint.setStrokeWidth(2);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        }
        if (heightMeasureSpec == MeasureSpec.EXACTLY) {
            height = heightSize;
        }
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        /**
         * 画大圆
         */
        paint.setStyle(Paint.Style.FILL);
        paint.setAlpha(0);
        paint.setColor(getResources().getColor(R.color.edit_gray_line));
        canvas.drawCircle(bigCircleX, bigCircleY, bigCircleR, paint);
        /**
         * 画小圆
         */
        paint.setStyle(Paint.Style.FILL);
        paint.setAlpha(0);
        paint.setColor(getResources().getColor(R.color.colorPrimary));
        canvas.drawCircle(smallCircleX, smallCircleY, smallCircleR, paint);

        /**
         * 添加上下左右四个箭头
         */
        Bitmap topBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.arrow_top_o);
        canvas.drawBitmap(topBitmap, bigCircleX - topBitmap.getWidth() / 2, bigCircleX - smallCircleR - topBitmap.getHeight() - 15, null);
        Bitmap leftBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.arrow_left_o);
        canvas.drawBitmap(leftBitmap, bigCircleX - smallCircleR - leftBitmap.getWidth() - 15, bigCircleX - leftBitmap.getHeight() / 2, null);
        Bitmap bottomBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.arrow_down_o);
        canvas.drawBitmap(bottomBitmap, bigCircleX - bottomBitmap.getWidth() / 2, bigCircleX + smallCircleR + 15, null);
        Bitmap rightBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.arrow_right_o);
        canvas.drawBitmap(rightBitmap, bigCircleX + smallCircleR + 15, bigCircleX - rightBitmap.getHeight() / 2, null);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) {
            /**
             * 记录触摸位置，根据这个歌位置来重绘小圆
             * 第一象限
             */
            if (event.getX() > width / 2 && event.getY() < height / 2) {
                if (Math.sqrt(Math.pow(event.getX() - width / 2, 2) + Math.pow(height / 2 - event.getY(), 2)) > bigCircleR) {
                    double sin = (height / 2 - event.getY()) / Math.sqrt(Math.pow(event.getX() - width / 2, 2) + Math.pow(height / 2 - event.getY(), 2));
                    double cos = (event.getX() - width / 2) / Math.sqrt(Math.pow(event.getX() - width / 2, 2) + Math.pow(height / 2 - event.getY(), 2));
                    smallCircleX = (float) (cos * bigCircleR + width / 2);
                    smallCircleY = (float) (height / 2 - sin * bigCircleR);
                } else {
                    smallCircleX = event.getX();
                    smallCircleY = event.getY();
                }
            } else if (event.getX() < width / 2 && event.getY() < height / 2) { //第二象限
                if (Math.sqrt(Math.pow(width / 2 - event.getX(), 2) + Math.pow(height / 2 - event.getY(), 2)) > bigCircleR) {   //圆外
                    double sin = (height / 2 - event.getY()) / Math.sqrt(Math.pow(width / 2 - event.getX(), 2) + Math.pow(height / 2 - event.getY(), 2));
                    double cos = (width / 2 - event.getX()) / Math.sqrt(Math.pow(width / 2 - event.getX(), 2) + Math.pow(height / 2 - event.getY(), 2));
                    smallCircleX = (float) (width / 2 - cos * bigCircleR);
                    smallCircleY = (float) (height / 2 - sin * bigCircleR);
                } else {    //圆内
                    smallCircleX = event.getX();
                    smallCircleY = event.getY();
                }
            } else if (event.getX() < width / 2 && event.getY() > height / 2) { //第三象限
                if (Math.sqrt(Math.pow(width / 2 - event.getX(), 2) + Math.pow(event.getY() - height / 2, 2)) > bigCircleR) {   //圆外
                    double sin = (event.getY() - height / 2) / Math.sqrt(Math.pow(width / 2 - event.getX(), 2) + Math.pow(event.getY() - height / 2, 2));
                    double cos = (width / 2 - event.getX()) / Math.sqrt(Math.pow(width / 2 - event.getX(), 2) + Math.pow(event.getY() - height / 2, 2));
                    smallCircleX = (float) (width / 2 - cos * bigCircleR);
                    smallCircleY = (float) (height / 2 + sin * bigCircleR);
                } else {    //圆内
                    smallCircleX = event.getX();
                    smallCircleY = event.getY();
                }
            } else if (event.getX() > width / 2 && event.getY() > height / 2) { //第四象限
                if (Math.sqrt(Math.pow(event.getX() - width / 2, 2) + Math.pow(event.getY() - height / 2, 2)) > bigCircleR) {   //圆外
                    double sin = (event.getY() - height / 2) / Math.sqrt(Math.pow(event.getX() - width / 2, 2) + Math.pow(event.getY() - height / 2, 2));
                    double cos = (event.getX() - width / 2) / Math.sqrt(Math.pow(event.getX() - width / 2, 2) + Math.pow(event.getY() - height / 2, 2));
                    smallCircleX = (float) (width / 2 + cos * bigCircleR);
                    smallCircleY = (float) (height / 2 + sin * bigCircleR);
                } else {    //圆内
                    smallCircleX = event.getX();
                    smallCircleY = event.getY();
                }
            }
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            /**
             * 在手指抬起离开屏幕后将小圆的位置还原
             */
            smallCircleX = width / 2;
            smallCircleY = height / 2;
        }
        /**
         * 添加移动方向监听器
         */
        if (onDirectionListener != null) {
            if (event.getY() < height / 4) {
                onDirectionListener.onUp();
            } else if (event.getY() > height / 4 * 3) {
                onDirectionListener.onDown();
            } else if (event.getX() < width / 4) {
                onDirectionListener.onLeft();
            } else if (event.getX() > width / 4 * 3) {
                onDirectionListener.onRight();
            }
        }
        /**
         * 重绘
         */
        invalidate();
        return true;
    }

    public interface OnDirectionListener {
        void onUp();

        void onDown();

        void onLeft();

        void onRight();
    }
}
